package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.AuthDto;
import com.heima.model.user.dtos.ApPageReqDto;
import com.heima.model.user.pojos.ApUserRealname;
import com.heima.user.mapper.ApUserAuthMapper;
import com.heima.user.service.ApUserAuthService;
import com.heima.user.service.ApUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

@Service
public class ApUserAuthServiceImpl extends ServiceImpl<ApUserAuthMapper, ApUserRealname>implements ApUserAuthService {

    @Autowired
    private KafkaTemplate<String,String> kafkaTemplate;

    @Autowired
    private ApUserService apUserService;
    /**
     * 用户审核列表查询
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult authList(ApPageReqDto dto) {
        //1.检查参数
        dto.checkParam();

        //2.分页查询
        IPage page = new Page(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<ApUserRealname> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //是否添加状态查询
        if (dto.getStatus()!=null) {
            lambdaQueryWrapper.eq(ApUserRealname::getStatus, dto.getStatus());
        }

        //按照时间倒序
        lambdaQueryWrapper.orderByDesc(ApUserRealname::getCreatedTime);


        page = page(page, lambdaQueryWrapper);

        //3.结果返回
        ResponseResult responseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());
        responseResult.setData(page.getRecords());
        return responseResult;
    }

    /**
     * 通过审核
     *
     * @return
     * @param dto
     */
    @Override
    public ResponseResult authPass(AuthDto dto) {
        //1.检查参数
        if (dto == null || dto.getId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2.修改
        update(Wrappers.<ApUserRealname>lambdaUpdate()
                .set(ApUserRealname::getStatus,9)
                .set(ApUserRealname::getReason,dto.getMsg())
                .eq(ApUserRealname::getId,dto.getId()));
        //3.向wemedia端添加新用户
//        dto.
//        apUserService.getOne(Wrappers.<ApUser>lambdaQuery().)
//        kafkaTemplate.send(KafkaConstants.ADD_WE_USER,"");
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 驳回审核
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult authFail(AuthDto dto) {
        //1.检查参数
        if (dto == null || dto.getId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.修改
        update(Wrappers.<ApUserRealname>lambdaUpdate()
                .set(ApUserRealname::getStatus,2)
                .set(ApUserRealname::getReason,dto.getMsg())
                .eq(ApUserRealname::getId,dto.getId()));
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
